package com.example.backend.filter;

import com.example.backend.mapper.FunctionMapper;
import com.example.backend.mapper.PostMapper;
import com.example.backend.service.FunctionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author Jcy
 * @Date 2022/5/13
 */
@Component
public class PostFilter implements HandlerInterceptor {
    @Autowired
    private FunctionMapper functionMapper;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        int postStatus = 1;
        if(functionMapper.selectById(postStatus).getFunction_status()==0){
        response.setContentType("application/json;charset=UTF-8");
        //设置编码格式
        response.setCharacterEncoding("UTF-8");
        response.setStatus(400);
        response.getWriter().write("{\n" +
                "\t\"resultCode\": 400,\n" +
                "\t\"message\": \"功能已锁定，请联系管理员解锁\",\n" +
                "\t\"data\": null\n" +
                "}");
            return false;
        }

    return true;
    }
}
